<?php

class %%Object%%s_model extends Model {
%%fieldDeclarationList%%

	function create($obj) {
		$this->data = $this->_objectToArray($obj);
		$this->db->insert('%%table%%', $this->data);
		return $this->db->insert_id();
	}


	function update($obj) {
		$this->data = $this->_objectToArray($obj);
		$this->db->where('%%pk%%', $obj->%%pk%%);
		$this->db->update('%%table%%', $this->data);
		return true;
	}

	function delete($obj) {
		$this->db->delete('%%table%%', array('%%pk%%' => $obj->%%pk%%));
	}


	function _baseFind() {
		$this->db->select('%%fieldList%%');
		$this->db->from('%%table%%');
		$this->db->orderby('%%pk%%', 'DESC');
	}


	function findAll() {
		$this->_baseFind();
		$this->db->orderby('%%pk%%', 'DESC');
		$query = $this->db->get();
		return $query->result();
	}


	function findByPrimaryKey($id) {
		$this->_baseFind();
		$this->db->where('%%pk%%', $id);
		$query = $this->db->get();
		$result = $query->result();
		if ($result == null) {
			return null;
		} else {
			return $this->_rowToObject($result[0]);
		}
	}


	function findByVal($col, $val) {
		$this->_baseFind();
		$this->db->where($col,$val);
		$query = $this->db->get();
		return $query->result();
	}

	function findByValPaginated($col, $val, $size=10, $offset) {
		$this->_baseFind();
		$this->db->where($col,$val);
		$this->db->limit($size, $offset);
		$query = $this->db->get();
		return $query->result();
	}


	function _rowToObject($row) {
		$obj = new %%Object%%s_model();
%%fieldsRowToObject%%
		return $obj;
	}
	
	function _objectToArray($obj) {
		$objArray = array();
%%fieldsObjectToArray%%	
		return $objArray;
	}
}

?>
